package com.boat.sample.ddd.infrastructure.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.boat.boat.support.api.controller.AccountApi;
import com.boat.boat.support.api.controller.NoticeApi;
import com.boat.boat.support.api.dto.account.AccountInfoRsp;
import com.boat.boat.support.api.dto.account.OpenAccountReq;
import com.boat.boat.support.api.dto.account.OpenAccountRsp;
import com.boat.boat.support.api.dto.notice.SendSMSReq;
import com.boat.boat.support.api.dto.notice.SendSMSRsp;
import com.boat.framework.base.api.Response;
import com.boat.framework.base.exception.ServiceException;
import com.boat.framework.common.util.JsonUtils;
import com.boat.starter.mockit.annotation.MockMapping;
import com.boat.starter.mockit.annotation.MockRequestBody;
import com.boat.starter.mockit.annotation.MockRequestQuery;
import com.boat.starter.mockit.annotation.Mockable;

import lombok.extern.slf4j.Slf4j;

/**
 * 支撑系统客户端
 * 
 * @author 周江
 *
 */
@Mockable(name = "[Mock]支撑系统客户端")
@Slf4j
@Component
public class SupportClient {
	@Autowired
	AccountApi accountApi;
	@Autowired
	NoticeApi noticeApi;

	@MockMapping(name = "[SLS001]开通账户", path = "/supportClient/openAccount")
	public OpenAccountRsp openAccount(@MockRequestBody OpenAccountReq request) {
		Response<OpenAccountRsp> response = accountApi.openAccount(request);
		if (!response.isSuccess()) {
			log.warn("调用开户操作失败。错误信息：{}", JsonUtils.toJson(response));
			throw new ServiceException(response.getCode(), response.getMessage());
		}
		return response.getData();
	}

	@MockMapping(name = "[SLS002]查询账号信息", path = "/supportClient/accountInfo")
	public AccountInfoRsp accountInfo(@MockRequestQuery(name = "userId", description = "用户Id") Integer userId) {
		Response<AccountInfoRsp> response = accountApi.accountInfo(userId);
		if (!response.isSuccess()) {
			log.warn("调用查询账户信息操作失败。错误信息：{}", JsonUtils.toJson(response));
			throw new ServiceException(response.getCode(), response.getMessage());
		}
		return response.getData();
	}

	public Response<AccountInfoRsp> accountInfo2(Integer userId) {
		return accountApi.accountInfo(userId);
	}

	@MockMapping(name = "[SLS003]发送短信", path = "/supportClient/sendSMS")
	public SendSMSRsp sendSMS(@MockRequestQuery(name = "mobile", description = "手机号") String mobile,
			@MockRequestQuery(name = "content", description = "短信内容") String content) {
		SendSMSReq request = new SendSMSReq();
		request.setMobile(mobile);
		request.setContent(content);

		Response<SendSMSRsp> response = noticeApi.sendSMS(request);
		if (!response.isSuccess()) {
			log.warn("调用查询账户信息操作失败。错误信息：{}", JsonUtils.toJson(response));
			throw new ServiceException(response.getCode(), response.getMessage());
		}
		return response.getData();
	}
}
